﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

/// <summary>
///ePioneer_Order 的摘要说明
/// </summary>
public class ePioneer_Order
{
    public ePioneer_Order()
    {
        //
        //TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 调用方法查询单个表的信息，并返回DataTable类型值的方法
    /// </summary>
    /// <param name="sql">sql语句或存储过程名</param>
    /// <param name="re">是否是存储过程</param>
    /// <param name="values">参数</param>
    /// <returns>返回DataTable</returns>
    private DataTable GetDataTable(string sql, bool re, params SqlParameter[] values)
    {
        string str = System.Configuration.ConfigurationManager.ConnectionStrings["emweb_new"].ConnectionString;
        //创建连接字符串对象
        SqlConnection con = new SqlConnection(str);
        //创建一个表对象
        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter(sql, con);

        if (re)
        {
            //定义命令类型
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
        }//if (re)

        if (values != null)
        {
            //添加参数
            da.SelectCommand.Parameters.AddRange(values);
        }//if (values != null)

        try
        {
            da.Fill(dt);
        }//try
        catch (SqlException ex)
        {
            //写错误日志
            throw ex;

        }//try catch(SqlException ex)
        finally
        {

            if (con.State == ConnectionState.Open)
            {
                //关闭连接
                con.Close();
            }//if (con.State == ConnectionState.Open)

        }//try catch(SqlException ex) finally

        return dt;

    }//public DataTable GetDataTable(string sql, bool re, params SqlParameter[] values)

    /// <summary>
    /// 调用方法查询一个或多个表的信息，并返回DataSet类型值的方法
    /// </summary>
    /// <param name="sql">sql语句或存储过程名</param>
    /// <param name="re">是否是存储过程</param>
    /// <param name="values">参数</param>
    /// <returns>返回DataSet</returns>
    public DataSet GetDataSet(string sql, bool re, params SqlParameter[] values)
    {
        string str = System.Configuration.ConfigurationManager.ConnectionStrings["emweb_new"].ConnectionString;
        //创建连接字符串对象
        SqlConnection con = new SqlConnection(str);
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sql, con);

        if (re)
        {
            //定义命令类型
            da.SelectCommand.CommandType = CommandType.StoredProcedure;
        }//if (re)//判断命令类型

        if (values != null)
        {
            //添加参数
            da.SelectCommand.Parameters.AddRange(values);
        }//if (values != null)//判断参数是否存在

        try
        {
            da.Fill(ds);

        }//try
        catch (SqlException ex)
        {
            //写错误日志
            throw ex;

        }//try catch(SqlException ex)捕获数据库异常
        finally
        {

            if (con.State == ConnectionState.Open)
            {
                //关闭连接
                con.Close();
            }//if (con.State == ConnectionState.Open)关闭连接

        }//try catch(SqlException ex) finally

        return ds;

    }//public DataSet GetDataSet(string sql, bool re, params SqlParameter[] values)

    public List<ePioneer_Order_Model> GetEPioneerOrder()
    {
        List<ePioneer_Order_Model> list = new List<ePioneer_Order_Model>();
        DataSet ds = GetDataSet("Web_EPioneer_Orders_Query", true, null);
        if (ds.Tables.Count > 0)
        {
            DataTable dt = ds.Tables[0];
            int counts = dt.Rows.Count;
            for (int i = 0; i < counts; i++)
            {
                ePioneer_Order_Model model = new ePioneer_Order_Model();
                model.Id = dt.Rows[i]["id"].ToString();
                model.Mobile = dt.Rows[i]["mobile"].ToString();
                model.Orderdate = dt.Rows[i]["orderdate"].ToString();
                model.Ordernumber = dt.Rows[i]["ordernumber"].ToString();
                model.Truename = dt.Rows[i]["truename"].ToString();
                model.Allmoney = dt.Rows[i]["allmoney"].ToString();
                DataRow[] dr = ds.Tables[1].Select("ordernumber='" + model.Ordernumber + "'");
                string details = "";
                for (int j = 0; j < dr.Length; j++)
                {
                    details += "产品名：" + dr[j]["productname"].ToString() + " 单价：" + dr[j]["nowprice"].ToString() + "元 数量：" + dr[j]["num"].ToString() + "<br>";
                }
                model.Detail = details + "总计：" + model.Allmoney + "元";
                list.Add(model);
            }
        }
        return list;
    }
}